﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Master_YigongSrhForm
 * 类型：       控制层
 * 功能说明：   义工检索页面
 * 作者：       徐傲明
 * 创建日期：   2008/02/22
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using log4net;
using Shpm.System;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Commons.Utils;
using Shpm.System.Validator;
using Shpm.App.Input.Bean;
using Shpm.System.Web.Control;

public partial class App_Master_YigongSrhForm : System.Web.UI.Page
{
    protected static ILog log = LogManager.GetLogger(typeof(App_Master_YigongSrhForm));
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SysUserManager manger = new SysUserManager(Context);
            SysUserBean sysUserBean = new SysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            //检索标志位
            this.ViewState["SEARCH_FLG"] = 0;

            initPageControls();

            //传递检索参数与页面编号
            if (Context.Items.Contains("YIGONG_NAME"))
            {
                this.ViewState["YigongName"] = Convert.ToString(this.Context.Items["YIGONG_NAME"]);
                this.txtName.Text = Convert.ToString(this.ViewState["YigongName"]);
            }
            if (Context.Items.Contains("SEX"))
            {
                this.ViewState["Sex"] = Convert.ToString(this.Context.Items["SEX"]);
                this.sex.SelectedValue = Convert.ToString(this.ViewState["Sex"]);
            }
            if (Context.Items.Contains("SEND_DATE"))
            {
                this.ViewState["SendDate"] = Convert.ToString(this.Context.Items["SEND_DATE"]);
                this.txtSendDate.Text = Convert.ToString(this.ViewState["SendDate"]);
            }
            if (Context.Items.Contains("SEND_DATE_TO"))
            {
                this.ViewState["SendDateTo"] = Convert.ToString(this.Context.Items["SEND_DATE_TO"]);
                this.txtSendDateTo.Text = Convert.ToString(this.ViewState["SendDateTo"]);
            }

            if (Context.Items.Contains("ParamPageIndex"))
            {
                this.ViewState["ParamPageIndex"] = Convert.ToInt32(this.Context.Items["ParamPageIndex"]);
                this.viewSrhRst.PageIndex = Convert.ToInt32(this.ViewState["ParamPageIndex"]);
            }
            if (Context.Items.Contains("Serch"))
            {
                this.ViewState["Serch"] = Convert.ToString(Context.Items["Serch"]);
            }
        }

    }

    private void initPageControls()
    {
        CommonDictionaryDao dao = new CommonDictionaryDao();
        CommonDictionaryBean bean = new CommonDictionaryBean();

        //数据词典加载
        bean.Catalog = "SEX";
        IList list = dao.FindByCatalog(bean);
        list.RemoveAt(0);
        list.Insert(0, new CommonDictionaryBean());
        sex.DataSource = list;
        sex.DataBind();
    }

    void Page_PreRender(object sender, EventArgs e)
    {
        if ("1".Equals(Convert.ToString(this.ViewState["SEARCH_FLG"])))
        {
            return;
        }

        YigongDao dao = new YigongDao();
        YigongBean bean = new YigongBean();
        IList list = null;

        //加载检索条件
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["YigongId"])))
        {
            bean.YigongId = Convert.ToInt32(this.ViewState["YigongId"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["YigongName"])))
        {
            bean.NameC = Convert.ToString(this.ViewState["YigongName"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["Sex"])))
        {
            bean.Sex = Convert.ToString(this.ViewState["Sex"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["SendDate"])))
        {
            bean.SendDate = Convert.ToDateTime(this.ViewState["SendDate"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["SendDateTo"])))
        {
            bean.SendDateTo = Convert.ToDateTime(this.ViewState["SendDateTo"]);
        }

        //数据检索
        list = dao.GetList(bean);

        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["delete"])))
        {
            this.ViewState["delete"] = null;
        }
        else
        {
            if (list == null || list.Count == 0 && !string.IsNullOrEmpty(Convert.ToString(this.ViewState["Serch"])))
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0005").ToString();
            }
        }

        viewSrhRst.DataSource = list;
        viewSrhRst.DataBind();
    }

    protected void txtAdd_Click(object sender, EventArgs e)
    {
        string url = "~/App/Master/YigongUpdForm.aspx";
        Context.Items["YIGONG_NAME"] = Convert.ToString(this.ViewState["YigongName"]);
        Context.Items["SEX"] = Convert.ToString(this.ViewState["Sex"]);
        Context.Items["SEND_DATE"] = Convert.ToString(this.ViewState["SendDate"]);
        Context.Items["SEND_DATE_TO"] = Convert.ToString(this.ViewState["SendDateTo"]);
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;

        //跳转到新页面
        ControllerUtils.Transfer(Context, url, true);
    }

    protected void btnSeach_Click(object sender, EventArgs e)
    {
        ComValidateChecker validator = new ComValidateChecker();

        //数据校验
        validator.Add(new CheckMaxCharLen(txtName.Text, 100, new ComMessage("E0321", new String[] { "义工姓名", "10" })));
        validator.Add(new CheckDate(txtSendDate.Text, "yyyy/MM/dd", new ComMessage("E0037", "出生日期")));
        validator.Add(new CheckDate(txtSendDateTo.Text, "yyyy/MM/dd", new ComMessage("E0037", "出生日期")));
        validator.Add(new CheckDateRange(txtSendDate.Text, txtSendDateTo.Text, new ComMessage("E0329", new string[] { "开始日期", "结束日期" })));
        //校验不通过
        if (!validator.DoValidator())
        {
            ((Design_MasterPage)this.Master).InfoMessage = validator.ErrorMessage;
            this.ViewState["SEARCH_FLG"] = 1;
            return;
        }

        //校验通过
        this.ViewState["SEARCH_FLG"] = 0;
        this.ViewState["YigongName"] = txtName.Text;
        this.ViewState["Sex"] = sex.SelectedValue;
        this.ViewState["SendDate"] = txtSendDate.Text;
        this.ViewState["SendDateTo"] = txtSendDateTo.Text;
        this.ViewState["Serch"] = "Serch";
    }

    protected void viewSrhRst_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //数据删除
        int index = Convert.ToInt32(e.RowIndex);
        DataKey key = viewSrhRst.DataKeys[index];
        int YigongId = Convert.ToInt32(key.Value);

        YigongDao dao = new YigongDao();
        YigongBean bean = new YigongBean();
        bean.YigongId = YigongId;
        dao.DelYigong(bean);
        ((Design_MasterPage)this.Master).InfoMessage =
            HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();
        this.ViewState["delete"] = "1";
    }

    protected void viewSrhRst_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string YigongId = null;

        //获取编辑所要的主键
        int index = Convert.ToInt32(e.NewEditIndex);
        DataKey key = viewSrhRst.DataKeys[index];
        YigongId = Convert.ToString(key.Value);

        string url = "~/App/Master/YigongUpdForm.aspx";

        //获取检索条件
        Context.Items["YIGONG_ID"] = YigongId;
        Context.Items["YIGONG_NAME"] = Convert.ToString(this.ViewState["YigongName"]);
        Context.Items["SEX"] = Convert.ToString(this.ViewState["Sex"]);
        Context.Items["SEND_DATE"] = Convert.ToString(this.ViewState["SendDate"]);
        Context.Items["SEND_DATE_TO"] = Convert.ToString(this.ViewState["SendDateTo"]);
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;

        //跳转到新页面
        ControllerUtils.Transfer(Context, url, true);
    }
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //翻页
        viewSrhRst.PageIndex = e.NewPageIndex;
    }
}

